import axios from "axios";

//登录
export const login = async (username, password) => {
    return await axios({
        method: 'post',
        url: '/api/users/login',
        data: {
            username, password
        }
    });
}

//注册
export const register = async (username, nickname, password, src, email) => {
    return await axios({
        method: 'post',
        url: '/api/users/register',
        data: {
            username, nickname, password, src, email
        }
    });
}

//获取所有用户
export const getUsers = async () => {
    return await axios({
        method: 'get',
        url: '/api/users/getUsers'
    });
}

//添加文章
export const addArticle = async (src, title, introduct, content, author_id, type, status) => {
    return await axios({
        method: 'post',
        url: '/api/list/addList',
        data: {
            src, title, introduct, content, author_id, type, status
        }
    });
}

//获取文章列表
export const getAllList = async (type, index, status) => {
    return await axios({
        method: 'post',
        url: '/api/list/getArtList',
        data: {
            type,
            index,
            status
        }
    });
}

//获取热门文章列表
export const getHotList = async () => {
    return await axios({
        method: 'get',
        url: '/api/list/getHotList'
    });
}

//获取文章内容
export const getInfo = async (id) => {
    return await axios({
        method: 'get',
        url: '/api/list/getInfo',
        params: {
            id
        }
    });
}

//图片上传
export const uploads = async (data, dir) => {
    return await axios({
        method: 'post',
        url: '/api/list/uploads',
        data: {
            data,
            dir
        }
    });
}

//获取分类列表
export const getTypes = async (type) => {
    return await axios({
        method: 'post',
        url: '/api/types/getTypeList',
        data: {
            type
        }
    });
}

//获取多级分类
export const getTypeSelect = async (key) => {
    return await axios({
        method: 'get',
        url: '/api/types/getTypeSelect',
        params: {
            key
        }
    });
}

// 添加分类
export const addType = async (type_name, parent_id, src) => {
    return await axios({
        method: 'post',
        url: '/api/types/addType',
        data: {
            type_name, parent_id, src
        }
    });
}

//添加评论
export const addComments = async (comment, userId, artId) => {
    return await axios({
        method: 'post',
        url: '/api/list/addComments',
        data: {
            comment, userId, artId
        }
    });
}

//获取评论内容
export const getComments = async (artId, index) => {
    return await axios({
        method: 'post',
        url: '/api/list/getComments',
        data: {
            artId, index
        }
    });
}

//时间戳转换日期格式
export const formatDate = (value) => {
    let date = new Date(value * 1000 / 1000);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
};

//搜索
export const search = async (key, index) => {
    return await axios({
        method: 'post',
        url: '/api/list/search',
        data: {
            key, index
        }
    });
};

//添加幻灯片
export const addSwipers = async (src, http, info) => {
    return await axios({
        method: 'post',
        url: '/api/base/addSwiper',
        data: {
            src, http, info
        }
    });
};

//获取幻灯片
export const getSwiper = async (key) => {
    return await axios({
        method: 'get',
        url: '/api/base/getSwiper'
    });
}

//删除幻灯片
export const deleteSwipers = async (id) => {
    return await axios({
        method: 'post',
        url: '/api/base/delSwiper',
        data: {
            id
        }
    });
};

//添加友链
export const addUrl = async (info, http) => {
    return await axios({
        method: 'post',
        url: '/api/base/addUrl',
        data: {
            info, http
        }
    });
};

//获取友链
export const getUrls = async (key) => {
    return await axios({
        method: 'get',
        url: '/api/base/getUrls'
    });
}

//删除友链
export const delUrl = async (id) => {
    return await axios({
        method: 'post',
        url: '/api/base/delUrl',
        data: {
            id
        }
    });
};
